﻿using Microsoft.Extensions.Logging;
using System.Net;
using System.Text;

namespace PmSoft.Utilities;

public static class HttpHelper
{
    private static HttpClient client = new HttpClient();

    public static async Task<HttpResult> GetAsync(string url)
    {
        HttpResult result = new();
        try
        {
            client = new HttpClient();
            client.DefaultRequestHeaders.Add("HeaderName", "HeaderValue"); // 添加 header
            var response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();
            result.Code = response.StatusCode;
            result.Data = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            result.Code = HttpStatusCode.NotFound;
            result.ErrorMsg = ex.Message;
        }
        return result;
    }

    public static async Task<HttpResult> GetAsync(string url,string headerName,string headerValue)
    {
        HttpResult result = new();
        try
        {
            client = new HttpClient();
            client.DefaultRequestHeaders.Add(headerName, headerValue); // 添加 header
            using var response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();
            result.Code = response.StatusCode;
            result.Data = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            result.Code = HttpStatusCode.NotFound;
            result.ErrorMsg = ex.Message;
        }
        return result;
    }

    public static async Task<HttpResult> PostAsync(string url, string requestBody)
    {
        HttpResult result = new();
        try
        {
            client = new HttpClient();
            using var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            result.Code = response.StatusCode;
            result.Data = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            result.Code = HttpStatusCode.NotFound;
            result.ErrorMsg = ex.Message;
        }
        return result;
    }

    public static async Task<HttpResult> PostAsync(string url, string requestBody, string headerName, string headerValue)
    {
        HttpResult result = new();
        try
        {
            client = new HttpClient();
            using var content = new StringContent(requestBody, Encoding.UTF8, "application/json");

            // 添加自定义的头部信息
            client.DefaultRequestHeaders.Add(headerName, headerValue);
            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            result.Code = response.StatusCode;
            result.Data = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            result.Code = HttpStatusCode.NotFound;
            result.ErrorMsg = ex.Message;
        }
        return result;
    }

    public static async Task<HttpResult> PutAsync(string url, string requestBody,string headerName, string headerValue)
    {
        HttpResult result = new();
        try
        {
            client = new HttpClient();
            using var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
            // 添加自定义的头部信息
            client.DefaultRequestHeaders.Add(headerName, headerValue);
            var response = await client.PutAsync(url, content);
            response.EnsureSuccessStatusCode();
            result.Code = response.StatusCode;
            result.Data = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            result.Code = HttpStatusCode.NotFound;
            result.ErrorMsg = ex.Message;
        }
        return result;
    }

    public static async Task<string> DeleteAsync(string url)
    {
        client = new HttpClient();
        var response = await client.DeleteAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    // 可以根据需要添加其他方法，如Patch、Head等
}

public class HttpResult
{
    public HttpStatusCode Code { get; set; }

    public string ErrorMsg { get; set; } = string.Empty;

    public object Data { get; set; }
}
